package com.elitel.frame.business.service.access;

import com.elitel.frame.base.service.SysPrjServiceService;
import com.elitel.frame.core.util.ApplicationContextHelper;
import com.elitel.frame.main.dao.AcAppMapper;
import com.elitel.frame.main.dao.AcAppServiceMapper;
import com.elitel.frame.main.dao.AcServiceFilterFieldMapper;
import com.elitel.frame.main.entity.AcApp;
import com.elitel.frame.main.entity.AcAppService;
import com.elitel.frame.main.entity.AcServiceFilterField;
import com.elitel.frame.main.entity.SysPrjservice;
import com.elitel.frame.main.entity.access.FilterSqlHandlerDTO;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.elitel.common.core.constant.RequestConstant.APP_CODE;
import static com.elitel.common.core.constant.RequestConstant.SERVICE_ID;

/**
 * @author hi_bj
 */
@Component
public class PrepareFilterSqlHandler {

    /**
     * 封装过滤数据SQL类
     * @return FilterSqlHandlerDTO
     */
    public FilterSqlHandlerDTO handlerParameter() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
        String serviceCode = request.getParameter(SERVICE_ID);
        String appCode = request.getParameter(APP_CODE);
        if (ObjectUtils.isEmpty(serviceCode) || ObjectUtils.isEmpty(appCode)) {
            return null;
        }
        //获取服务基础信息
        SysPrjServiceService sysPrjServiceService = ApplicationContextHelper.getApplicationContext().getBean(SysPrjServiceService.class);
        SysPrjservice sysPrjservice = sysPrjServiceService.getSysPrjserviceByServiceCode(serviceCode);
        //获取过滤字段信息
        AcServiceFilterFieldMapper acServiceFilterFieldMapper = ApplicationContextHelper.getApplicationContext().getBean(AcServiceFilterFieldMapper.class);
        List<AcServiceFilterField> acServiceFilterFieldList = acServiceFilterFieldMapper.selectByServiceId(sysPrjservice.getGuid());
        if (ObjectUtils.isEmpty(acServiceFilterFieldList)) {
            return null;
        }
        //获取访问控制-应用和服务关系
        AcAppServiceMapper acAppServiceMapper = ApplicationContextHelper.getApplicationContext().getBean(AcAppServiceMapper.class);
        List<AcAppService> appServiceList = acAppServiceMapper.getAllByServiceId(sysPrjservice.getGuid());
        //获取访问控制-应用基础信息
        AcAppMapper acAppMapper = ApplicationContextHelper.getApplicationContext().getBean(AcAppMapper.class);
        AcApp acApp = acAppMapper.selectByAppCode(appCode);
        AcAppService curAcAppService = null;
        for (AcAppService acAppService : appServiceList) {
            if (acAppService.getAppId().equals(acApp.getAppId())) {
                curAcAppService = acAppService;
                break;
            }
        }
        if (ObjectUtils.isEmpty(curAcAppService)){
            return null;
        }
        //封装 过滤SQL处理对象
        FilterSqlHandlerDTO filterSqlHandler = new FilterSqlHandlerDTO();
        filterSqlHandler.setFilterStationFlag(false);
        filterSqlHandler.setFilterTimeFlag(false);
        filterSqlHandler.setAppCode(appCode);
        filterSqlHandler.setServiceId(serviceCode);
        filterSqlHandler.setFilterFiledList(acServiceFilterFieldList);
        //处理测站
        if (!ObjectUtils.isEmpty(curAcAppService.getFilterCatalogId())) {
            filterSqlHandler.setFilterStationFlag(true);
        }
        //处理开始时间
        if (!ObjectUtils.isEmpty(curAcAppService.getFilterStartTime())) {
            filterSqlHandler.setFilterTimeFlag(true);
            filterSqlHandler.setFilterStartTime(curAcAppService.getFilterStartTime());
        }
        //处理结束时间
        if (!ObjectUtils.isEmpty(curAcAppService.getFilterEndTime())) {
            filterSqlHandler.setFilterTimeFlag(true);
            filterSqlHandler.setFilterEnterTime(curAcAppService.getFilterEndTime());
        }
        return filterSqlHandler;
    }
}
